package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpDassessModelService;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpDassessModelMapper;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpDassessModel;
import java.util.stream.Stream;
@Service
public class PmpDassessModelServiceImpl implements IPmpDassessModelService{
 	@Autowired
	private PmpDassessModelMapper pmpDassessModelMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createPmpDassessModel(PmpDassessModel pmpDassessModel) {
		ZBeanUtil.setCreateInfo(pmpDassessModel);
		ZBeanUtil.validateBean(pmpDassessModel);
		pmpDassessModelMapper.insertSelective(pmpDassessModel);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<PmpDassessModel> loadPmpDassessModelById(Long id) {
		if(id!=null){
			return Optional.ofNullable(pmpDassessModelMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePmpDassessModel(PmpDassessModel pmpDassessModel) {
		ZBeanUtil.setUpdatedInfo(pmpDassessModel);
		ZBeanUtil.validateBean(pmpDassessModel);
		pmpDassessModelMapper.updateByPrimaryKeySelective(pmpDassessModel);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletePmpDassessModel(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		pmpDassessModelMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPmpDassessModelStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		pmpDassessModelMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadPmpDassessModelDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(pmpDassessModelMapper.selectListCount(pm), pmpDassessModelMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadComboboVo(boolean firstEmpty) {
		List<ComboboVo> voList=pmpDassessModelMapper.selectComboboVo();
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}
	
}